﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExpenseSystem.Model;
using ExpenseSystem.Fether;
using ExpenseSystem.DataUpdater;
using ExpenseSystem.Common;
using ExpenseSystem.Core.Authority;
using ExpenseSystem.DataUpdater.Validation;

namespace ExpenseSystem.Core.BonusModules
{
    public class OtherUpdaterRegister
    {
        public NormalDataUpdater<OtherModel> GetOtherUpdater()
        {
            NormalDataUpdater<OtherModel> systemDataUpdater = new NormalDataUpdater<OtherModel>();
            systemDataUpdater.DataDeleting += CheckStatus;
            systemDataUpdater.DataUpdating += SingletonValid;
            systemDataUpdater.DataUpdating += DataNotSpaceValid;
            systemDataUpdater.DataUpdating += DempartmentAndEmployeeExist;
            systemDataUpdater.DataUpdating += StateValid;
            systemDataUpdater.DataUpdating += DateValid;
            return systemDataUpdater;
        }
        private void CheckStatus(object sender, DataValidationEventArgs<OtherModel> e)
        {
            if (e.Delete.State != Definition.STATE_NORMAL)
            {
                e.Cancle = true;
                e.Message = "非普通状态不允许删除";
            }
        }
        private void SingletonValid(object sender, DataValidationEventArgs<OtherModel> e)
        {
            string message;
            OtherFetcher otherFetcher = new OtherFetcher();
            OtherModel other = otherFetcher.GetByCode(e.Insert.Site, e.Insert.Code, out message);
            if (other != null && other.AutoId != e.Insert.AutoId)
            {
                e.Message = String.Format("单号{0}已经被使用", e.Insert.Code);
                e.Cancle = true;
            }
        }
        private void DataNotSpaceValid(object sender, DataValidationEventArgs<OtherModel> e)
        {
            if (String.IsNullOrWhiteSpace(e.Insert.Code))
            {
                e.Message = "单号不允许为空白";
                e.Cancle = true;
                return;
            }
            if(e.Insert.Amount==0)
            {
                e.Message = "金额不允许为0";
                e.Cancle = true;
                return;
            }
        }

        private void DempartmentAndEmployeeExist(object sender, DataValidationEventArgs<OtherModel> e)
        {
            EmployeeFetcher employeeFetcher = new EmployeeFetcher();
            DepartmentFetcher departmentFetcher = new DepartmentFetcher();
            string message;
            if( employeeFetcher.Get(e.Insert.Site, e.Insert.EmployeeId,out message)==null)
            {
                e.Message = "使用了不存在的人员";
                e.Cancle = true;
                return;
            }
            if (departmentFetcher.Get(e.Insert.Site,e.Insert.DepartmentId, out message) == null)
            {
                e.Message = "使用了不存在的部门";
                e.Cancle = true;
                return;
            }
        }
        private void StateValid(object sender, DataValidationEventArgs<OtherModel> e)
        {
            if (e.Insert.State != Definition.STATE_NORMAL
                && (e.Insert.PropertyUpdated.Contains("DocDate") ||
                e.Insert.PropertyUpdated.Contains("Code") ||
                e.Insert.PropertyUpdated.Contains("EmployeeId") ||
                e.Insert.PropertyUpdated.Contains("DepartmentId") ||
                e.Insert.PropertyUpdated.Contains("Amount") 
                ))
            {
                e.Cancle = true;
                e.Message = "已提审/审核的单据不允许进行修改";
                return;
            }
        }
        private void DateValid(object sender, DataValidationEventArgs<OtherModel> e)
        {
            string message;
            FiscalPeriodFetcher fiscalPeriodFetcher = new FiscalPeriodFetcher();
            FiscalPeriodModel fiscalPeriodModel = fiscalPeriodFetcher.GetDefault(e.Insert.Site, out message);
            if (fiscalPeriodModel == null)
            {
                e.Cancle = true;
                e.Message = String.Format("无法取得财务周期，单据无法保存:{0}", message);
                return;
            }
            DateTime dt1, dt2, dt3;
            dt1 = dt2 = dt3 = DateTime.MinValue;
            bool b1 = DateTime.TryParse(e.Insert.DocDate, out dt1);
            bool b2 = DateTime.TryParse(fiscalPeriodModel.OpenDate, out dt2);
            bool b3 = DateTime.TryParse(fiscalPeriodModel.DueDate, out dt3);
            if (!(b1 && b2 && b3))
            {
                e.Cancle = true;
                e.Message = "日期格式错误";
                return;
            }
            if (dt1 < dt2 || dt1 > dt3)
            {
                e.Cancle = true;
                e.Message = String.Format("单据日期{0}不在财务区间{1}-{2}内", dt1, dt2, dt3);
                return;
            }
        }
    }
}
